//
//  Brands_BrandsMain_VC_WX.m
//  ZITHER
//
//  Created by weixin on 2019/4/8.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Brands_BrandsMain_VC_WX.h"

@implementation Brands_BrandsMain_VC_WX
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    self.model = [[GoodsBrandTagstListModel alloc]init];
    [self setLoadData];
}

-(void)setLoadData{
    [[serviceDefualt loadGoodsBrandTagstList:[GoodsBrandTagstListModel class]]subscribeNext:^(ResponseData * x) {
        if (x.success) {
            
            
            self.model.data = [[NSMutableArray alloc]init];
            NSArray *arr = x.responseRawData[@"data"];
            self.threadSafetyType = arr.count;

            for (int i =0; i<arr.count; i++) {
                GoodsBrandTagstListSubModel *model = [GoodsBrandTagstListSubModel yy_modelWithDictionary:arr[i]];
                [[serviceDefualt loadGoodsBrandTagst:model.id reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        
                        @try{
                            //代码写在这个里面
                            NSArray *arr1 = x.responseRawData[@"data"];
                            model.modelArrM = [[NSMutableArray alloc]init];
                            for (int j =0; j<arr1.count; j++) {
                                
                                GoodsBrandTagstModel *model1 = [GoodsBrandTagstModel yy_modelWithDictionary:arr1[j]];
                                [model.modelArrM addObject:model1];
                            }
                            self.threadSafetyType = self.threadSafetyType - 1;
                            
                            [self.model.data addObject:model];
                            
                            if (self.threadSafetyType ==0) {
                                [self setUI];
                                self.WxCollectionV.model = self.model;
                                NSLog(@"所有线程请求完毕");
                            }else{
                                NSLog(@"还有数据在请求");
                            }
                        }
                        @catch(NSException *exception) {
                            NSLog(@"异常错误是:%@", exception);
                            
                        }
                        @finally {
                            
                        }
                     
                
                    }else{
                        [Tools showTips:x.errorInfo];
                    }
                    
                }];
                
             
                
            }

            
        }else{
            [Tools showTips:x.errorInfo];
        }
    }];
    
    
    
//#pragma mark - ---  品牌馆标签
//    +(RACSignal *)loadGoodsBrandTagstList:(Class)reponseObjctClass;
//
//#pragma mark - --- 品牌馆对应标签获取品牌
//    +(RACSignal *)loadGoodsBrandTagst:(NSString*)BrandId reponseObjctClass:(Class)reponseObjctClass;

}




-(void)setUI{
    [self.view addSubview:self.BtnScroll];
    [self.view addSubview:self.WxCollectionV];
    NSMutableArray *arrTitle = [[NSMutableArray alloc]init];
    [arrTitle addObject:@"品牌"];
    for (GoodsBrandTagstListSubModel *model in self.model.data) {
        [arrTitle addObject:model.name];
    }
    self.BtnScroll.arrM = [[NSMutableArray alloc]initWithArray:arrTitle];
    self.WxCollectionV.arrTitle = [[NSMutableArray alloc]initWithArray:arrTitle];

}

-(void)setNav{
    
    /* TODO: ================= 导航栏V4样式 =================*/
    [self.baseNavigationView NavigationBarStyle:1];
    self.baseNavigationView.leftButton1.hidden = YES;
    self.baseNavigationView.navViewStyle.title.text = @"品牌馆";
    [self.baseNavigationView setHiddenNav:NO VC:self];
    
}


-(Brands_BrandsMainTitle_Scr_WX*)BtnScroll
{
    if (_BtnScroll) {
        return _BtnScroll;
    }
    _BtnScroll = [[Brands_BrandsMainTitle_Scr_WX alloc]initWithFrame:CGRectMake(0, NaviHeight,kScreenW, 80)];
    @weakify(self);
    [_BtnScroll setBlock:^(NSUInteger index) {
        @strongify(self);
        
        [self.WxCollectionV setContentOffset:CGPointMake(kScreenW*(index-100), 0) animated:YES];
        
    }];
    return _BtnScroll;
}

-(Brands_BrandsMain_Collection_WX *)WxCollectionV
{
    if (_WxCollectionV) {
        return _WxCollectionV;
    }
    
    CGFloat h = kScreenH-NaviHeight-80;
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    
    layout.minimumInteritemSpacing=0;
    
    layout.minimumLineSpacing = 0;
    
    layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;

    layout.itemSize = CGSizeMake(SCREEN_WIDTH,h);
    
    _WxCollectionV = [[Brands_BrandsMain_Collection_WX alloc]initWithFrame:CGRectMake(0, NaviHeight+80, SCREEN_WIDTH, h) collectionViewLayout:layout];
    
    @weakify(self);
    [_WxCollectionV setBlock:^(NSInteger index) {
        @strongify(self);
        NSLog(@"%ld.index ",index);
        [self.BtnScroll changeBtn:index+100];
//        [self refdata:index];
    }];
    
    [RACObserve(self.WxCollectionV,contentOffset)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        int newContent = self.WxCollectionV.contentOffset.x;
        int width = kScreenW;
        if (newContent%width == 0) {
//            [self refdata:newContent/width];
        }
        
    }];
    return _WxCollectionV;
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}


@end
